/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package gm

import (
	"fmt"
	"testing"
)

func TestSm2(t *testing.T) {
	prvKeyHex, pubKeyHex, _ := GenerateSM2Keys()
	fmt.Printf("prvKey: %s\npubKey: %s\n", prvKeyHex, pubKeyHex)

	message := "112233445"

	sign := Sign(prvKeyHex, []byte(message))
	verify := Verify(pubKeyHex, []byte(message), sign)
	fmt.Printf("verify: %t\n", verify)

	encryptedData := EncryptByPubKey(pubKeyHex, []byte(message))
	fmt.Printf("encryptedData: %s\n", encryptedData)
	src := DecryptByPrvKey(prvKeyHex, encryptedData)
	fmt.Printf("src: %s\n", string(src))
}

func TestSm3(t *testing.T) {
	message := "asdfasdfasdfasdfasdfasdfasfadsf"
	result := Sm3(message)
	fmt.Printf("result: %s\n", result)
}

func TestSm4(t *testing.T) {
	message := "asdfasdfadsfasdf1221312312334512351235"
	key := GenerateSm4Key()
	fmt.Printf("key: %s\n", key)
	encryptData := Sm4Encrypt(key, []byte(message))
	decryptData := Sm4Decrypt(key, encryptData)
	fmt.Printf("decryptData: %s\n", string(decryptData))

	prvKeyHex, _, _ := GenerateSM2Keys()
	fmt.Printf("prvKeyHex: %s\n", prvKeyHex)
	encryptedPrvKey := EncryptPrvKey(prvKeyHex)
	fmt.Printf("encryptedPrvKey: %s\n", encryptedPrvKey)
	prvKey := DecryptPrvKey(encryptedPrvKey)
	fmt.Printf("prvKeyHex: %s\n", prvKey)

	srcFile := "D:/5.png"
	destFile := "D:/5_encrypted.png"
	destFile1 := "D:/5_decrypted.png"

	err := Sm4EncryptFile(key, srcFile, destFile)
	if err != nil {
		panic(err)
	}
	err = Sm4DecryptFile(key, destFile, destFile1)
	if err != nil {
		panic(err)
	}
}
